package com.calvin.combat;

import java.net.URL;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.calvin.combat.service.MMService;
import com.calvin.combat.service.MMService.MMBinder;
import com.calvin.combat.view.BaseActivity;
import com.calvin.combat.view.WebActivity;

public class MainActivity extends BaseActivity implements OnClickListener {
	private Button btn_Start, btn_Stop, btn_Bind, btn_UnBind, btn_Navigate;
	private MMBinder mBinder;
	private boolean serviceIsBind = false;
	private String TAG = "MainActivity";

	// private MMBinder mBinder;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Log.i(TAG, "--->> MainActivity Threed id is "
				+ Thread.currentThread().getId());
		btn_Start = (Button) this.findViewById(R.id.btn_StartService);
		btn_Stop = (Button) this.findViewById(R.id.btn_StopService);
		btn_Bind = (Button) this.findViewById(R.id.btn_BindService);
		btn_UnBind = (Button) this.findViewById(R.id.btn_UnBindService);
		btn_Navigate = (Button) this.findViewById(R.id.btn_navigateToWeb);
		btn_Start.setOnClickListener(this);
		btn_Stop.setOnClickListener(this);
		btn_Bind.setOnClickListener(this);
		btn_UnBind.setOnClickListener(this);
		btn_Navigate.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case 1:
			if (resultCode == RESULT_OK) {
				String result = data.getStringExtra("data_return");
				Toast.makeText(this, result, Toast.LENGTH_SHORT).show();
			}
			break;

		default:
			break;
		}
	}

	@Override
	public void onClick(View v) {
		int action = v.getId();
		switch (action) {
		case R.id.btn_StartService:
			Intent startIntent = new Intent(this, MMService.class);
			startService(startIntent);
			break;
		case R.id.btn_StopService:
			Intent stopIntent = new Intent(this, MMService.class);
			stopService(stopIntent);
			break;
		case R.id.btn_BindService:
			Intent bindIntent = new Intent(this, MMService.class);
			serviceIsBind = bindService(bindIntent, serviceConnection,
					BIND_AUTO_CREATE);
			break;
		case R.id.btn_UnBindService:
			if (serviceIsBind) {
				unbindService(serviceConnection);
				serviceIsBind = false;
			}
			break;
		case R.id.btn_navigateToWeb:
			Intent webIntent = new Intent(this, WebActivity.class);
			webIntent.putExtra("MainActivity", "Hi ");
			startActivityForResult(webIntent, 1);
			break;
		default:
			break;
		}
	}

	private ServiceConnection serviceConnection = new ServiceConnection() {

		@Override
		public void onServiceDisconnected(ComponentName name) {

		}

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			mBinder = (MMBinder) service;
			mBinder.startDownload();
		}
	};
}
